package com.ruoyi.project.system.controller;

import java.util.ArrayList;
import java.util.List;

import java.util.Map;
import java.util.stream.Collectors;


import com.ruoyi.project.system.domain.vo.RegionVo;

import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysCity;
import com.ruoyi.project.system.service.ISysCityService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;


/**
 * 城市Controller
 *
 * @author jjp
 * @date 2020-04-09
 */
@RestController
@RequestMapping("/system/city")
public class SysCityController extends BaseController {
    @Autowired
    private ISysCityService sysCityService;

/*    *//**
     * 查询城市列表
     *//*
    @PreAuthorize("@ss.hasPermi('system:city:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysCity sysCity) {
        startPage();
        List<SysCity> list = sysCityService.selectSysCityList(sysCity);
        return getDataTable(sysCityService.buildTypeTree(list));
    }*/


    /**
     * 获取城市详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:city:query')")
    @GetMapping(value = "/cityMsg/{cityId}")
    public AjaxResult getInfo(@PathVariable("cityId") Long cityId)
    {
        AjaxResult success = AjaxResult.success(sysCityService.selectSysCityById(cityId));
        return AjaxResult.success(success);
    }


    @ApiOperation("查询列表")
    @GetMapping("/list")
    public AjaxResult queryThreeCity() {
        List<SysCity> sysAreas2 = sysCityService.queryAllCity();
        Map<Long, List<SysCity>> collect = sysAreas2.stream().collect(Collectors.groupingBy(SysCity::getLevel));
        long ProvinceLevel = 1;
        List<SysCity> provinces = collect.get(ProvinceLevel);
        long cityLevel = 2;
        List<SysCity> citys = collect.get(cityLevel);
        long districtLevel = 3;
        List<SysCity> districts = collect.get(districtLevel);
/*
        List<SysArea> provinces = sysAreaService.queryProvince();
        List<SysArea> citys = sysAreaService.queryCity();
        List<SysArea> districts = sysAreaService.queryDistrict();
*/
        List<RegionVo> provinceList = new ArrayList();
        Map<Long, List<SysCity>> cityListMap = citys.stream().collect(Collectors.groupingBy(SysCity::getParentCityId));

        Map<Long, List<SysCity>> districtMap = districts.stream().collect(Collectors.groupingBy(SysCity::getParentCityId));

        try {
            for (SysCity province: provinces) {
                RegionVo provinceVo = new RegionVo();
                provinceVo.setId(province.getCityId());
                provinceVo.setName(province.getCityName());
                List<RegionVo> cityList = new ArrayList();
                List<SysCity> sysAreas = cityListMap.get(province.getCityId());

                for (SysCity city: sysAreas) {
                    RegionVo cityVo = new RegionVo();
                    cityVo.setId(city.getCityId());
                    cityVo.setName(city.getCityName());
                    List<RegionVo> districtList = new ArrayList();
                    List<SysCity> sysAreas1 = districtMap.get(city.getCityId());

                    for (SysCity district: sysAreas1) {
                        RegionVo districtVo = new RegionVo();
                        districtVo.setId(district.getCityId());
                        districtVo.setName(district.getCityName());
                        districtList.add(districtVo);
                    }
                    cityVo.setChildren(districtList);
                    cityList.add(cityVo);
                }
                provinceVo.setChildren(cityList);
                provinceList.add(provinceVo);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.success(provinceList);
    }


    /**
     * 新增城市
     */
    @PreAuthorize("@ss.hasPermi('system:city:add')")
    @Log(title = "城市", businessType = BusinessType.INSERT)
    @PostMapping("/addNewCity")
    public AjaxResult add(@RequestBody SysCity sysCity)
    {
        List<SysCity> sysCities = sysCityService.queryByCityName(sysCity);
        if(sysCities.size() != 0){
            return AjaxResult.error("不可重复添加");
        }
        return toAjax(sysCityService.insertSysCity(sysCity));
    }

    /**
     * 修改城市
     */
    @PreAuthorize("@ss.hasPermi('system:city:edit')")
    @Log(title = "城市", businessType = BusinessType.UPDATE)
    @PutMapping("/updateCity")
    public AjaxResult edit(@RequestBody SysCity sysCity)
    {
        System.out.println(sysCity.getCityName());
        return toAjax(sysCityService.updateSysCity(sysCity));
    }

    /**
     * 删除城市
     */
    @PreAuthorize("@ss.hasPermi('system:city:remove')")
    @Log(title = "城市", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteCity/{cityIds}")
    public AjaxResult remove(@PathVariable Long[] cityIds)
    {
        return toAjax(sysCityService.deleteSysCityByIds(cityIds));
    }

    /**
     * 通过城市查服务
     */
    @PostMapping("/cityService")
    public AjaxResult CityService(@RequestBody SysCity sysCity) {
        if(!"".equals(sysCity.getCityName())){
            List<SysCity> sysCities1 = sysCityService.selectSysCityList(sysCity);
            List<SysCity> sysCities = sysCityService.QueryCityService(sysCities1.get(0).getCityId());
            return AjaxResult.success(sysCities);
        }
        return AjaxResult.success(sysCityService.QueryCityService(sysCity.getCityId()));
    }
}
